var express = require('express');
var session = require('express-session');
var path = require('path');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();

/* GET users listing. */
router.get('/', function (req, res, next) {
  var db = new sqlite3.Database(path.join(__dirname, "../db/data.s3db"));
  var username = req.query.username;
  var mode = req.query.mode;
  var redata = {};
  if (mode == '1') { //查询所有工地信息 表格分页展示
    var size = 5;
    var page = req.query.page;
    var num = 0;
    try {
      db.serialize(function () {
        sqlStr = "SELECT * from workplace where username=='" + username + "'";
        db.all(sqlStr, function (err, rows) {
          redata.count = rows.length;
          redata.code = '0';
          redata.data = [];
          if (rows.length <= size)
            redata.data = rows;
          else {
            N = rows.length - size * (page - 1);
            if (N >= size)
              for (i = size * (page - 1); i < size * page; i++) {
                redata.data[num] = rows[i];
                num++;
              }
            else
              for (i = size * (page - 1); i < size * (page - 1) + N; i++) {
                redata.data[num] = rows[i];
                num++;
              }
          }
          res.send(redata);
        });
      });
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '2') { //增加一个工地
    var pname = req.query.pname;
    var pname;
    if (req.query.workplace == '0')
      pname = '旭辉运河悦章';
    var site = req.query.site;
    var siteDetail = req.query.siteDetail;
    var workername = req.query.workername;
    var workerphone = req.query.workerphone;
    var workername2 = req.query.workername2;
    var workerphone2 = req.query.workerphone2;
    try {
      db.run("INSERT INTO workplace(username,pname,site,siteDetail,workername,workerphone,workername2,workerphone2) VALUES('" + username + "','" + pname + "','" + site + "','" + siteDetail + "','" + workername + "','" + workerphone + "','" + workername2 + "','" + workerphone2 + "')");
      res.send('1');
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '3') {
    try {
      db.serialize(function () {
        sqlStr = "SELECT * from workplace where username=='" + req.query.username + "' and pid=='" + req.query.pid + "'";
        db.all(sqlStr, function (err, rows) {
          redata = rows[0];
          res.send(redata);
        });
      });
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '4') {
    var pid = req.query.pid;
    var pname = req.query.pname;
    var siteDetail = req.query.siteDetail;
    var workername = req.query.workername;
    var workerphone = req.query.workerphone;
    var workername2 = req.query.workername2;
    var workerphone2 = req.query.workerphone2;
    try {
      db.run("UPDATE workplace SET pname ='" + pname + "', siteDetail='" + siteDetail + "', workername='" + workername + "', workerphone='" + workerphone + "', workername2='" + workername2 + "', workerphone2='" + workerphone2 + "' WHERE pid=='" + pid + "' and username=='" + username + "'");
      res.send('1');
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '5') {
    try {
      db.serialize(function () {
        if (req.query.workplace != '个人身份')
          sqlStr = "SELECT * from workplace where username=='" + req.query.username + "' and pname=='" + req.query.workplace + "'";
        else
          sqlStr = "SELECT * from receiveInfo";
        db.all(sqlStr, function (err, rows) {
          redata.data = rows;
          if (rows != undefined)
            redata.count = rows.length;
          redata.code = '0';
          res.send(redata);
        });
      });
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '6') {
    var pid = req.query.pid;
    var siteDetail = req.query.siteDetail;
    var workername = req.query.workername;
    var workerphone = req.query.workerphone;
    var flag = req.query.flag;
    try {
      if (flag == '0') {
        db.run("UPDATE workplace SET siteDetail='" + siteDetail + "', workername='" + workername + "', workerphone='" + workerphone + "' WHERE pid=='" + pid + "'");
      } else
        db.run("UPDATE receiveInfo SET siteDetail='" + siteDetail + "', workername='" + workername + "', workerphone='" + workerphone + "' WHERE pid=='" + pid + "'");
      res.send('1');
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '7') { //增加一个工地
    var flag = req.query.flag;
    if (flag == '0')
      var pname = '旭辉运河悦章';
    var siteDetail = req.query.siteDetail;
    var workername = req.query.workername;
    var workerphone = req.query.workerphone;
    try {
      if (flag == '0')
        db.run("INSERT INTO workplace(username,pname,siteDetail,workername,workerphone) VALUES('" + username + "','" + pname + "','" + siteDetail + "','" + workername + "','" + workerphone + "')");
      else
        db.run("INSERT INTO receiveInfo(siteDetail,workername,workerphone) VALUES('" + siteDetail + "','" + workername + "','" + workerphone + "')");
      res.send('1');
    } catch (ex) {
      console.log(ex);
    }
  } else if (mode == '8') {
    try {
      db.serialize(function () {
        sqlStr = "SELECT * from workplace where username=='" + req.query.username + "' and pname=='" + req.query.workplace + "'";
        redata = {
          code: '0',
          count: 1,
          data: []
        };
        db.all(sqlStr, function (err, rows) {
          redata.data[0] = rows[0];
          res.send(redata);
        });
      });
    } catch (ex) {
      console.log(ex);
    }
  }
});

module.exports = router;